Forward Declare #include

I have some code which works fine on its own but when called by #include does not work. From browsing the forum I believe I need to "forward declare" some variables but am stuck and hope someone can help.

I have placed below a simple dialog box to demonstrate the problem.
On selection of an item in the list I need to populate the "Specific Value" box however this variable (I believe) cannot be declared globally and declaring this variable in either function does not work.

Martin

//FILE 1
 
DBE xVALSel = null
 
void doSelect(DBE xVALOpt)
{
    //DBE xVALSel = null
        
        string xVAL = get xVALOpt
        set (xVALSel, xVAL)
}
 
string guessAndAsk ()
{
        string xVALChoice[]  = {"A", "B", "C", "D"}
        DB  xVALDB       = null
        DBE xVALOpt      = null
        //DBE xVALSel      = null
        DBE applyBtn     = null
 
        xVALDB = create("", styleCentered|styleFloating)
        
        xVALOpt  = list (xVALDB, "", 100, 5, xVALChoice)
        xVALSel = field (xVALDB, "Specific Value: ", "" ,4)
        
        realize xVALDB
        
        set (xVALOpt, doSelect)
        
        block xVALDB
 
        return get xVALSel
        
}
 
//FILE 2
 
//#include <FILE 1>
 
guessAndAsk ()

Martin_Hunter - Tue Dec 07 11:33:07 EST 2010

Re: Forward Declare #include
llandale - Tue Dec 07 11:53:10 EST 2010

Since callback functions routinely deal with DBEs, you should routinely declare your DB and DBE variables globally, unless your box is very simple and doesn't require anything tricky. Also, forgive ignorance but I suspect there may be an issue with declaring your xValChoice[] vector inside the function, as its de-allocated when the function ends. I'd declare that globally as well.

The following works in an included file.

DB  xVALDB       = null
DBE xVALOpt      = null
DBE xVALSel      = null
DBE applyBtn     = null
string xVALChoice[]  = {"A", "B", "C", "D"}
 
void doSelect(DBE xVALOpt)
{
    string xVAL = get xVALOpt
        set (xVALSel, xVAL)
}
 
string guessAndAsk ()
{
        xVALDB = create("", styleCentered|styleFloating)
        xVALOpt  = list (xVALDB, "", 100, 5, xVALChoice)
        xVALSel = field (xVALDB, "Specific Value: ", "" ,4)
        realize xVALDB
        set (xVALOpt, doSelect)
        block xVALDB
        string RetValue = get xVALSel
        hide(xVALDB)
        destroy(xVALDB)
        xVALDB = null
        return RetValue
}


Now lets not get whipetty-snippetty. This will not work if your included file is inside some function in the main code, since the 'global' variables in the include will no longer be 'global' and your functions, strangely, can access only local or global variables, they cannot access variables declared in an intermediate context. That's a strange 'feature' of DXL. Anyway, you won't need to: $include this in the main and call guessAndAsk from some function will work.

Now of course this #include file will need to insure its global variables don't accidentally conflict with the main calling variables. You may want to adopt a naming convention, perhaps all your library functions start with "f" and all your library variables start with "cl_" or "gl_".

 

 

  • Louie

 

 

Re: Forward Declare #include
Martin_Hunter - Tue Dec 07 12:33:34 EST 2010

llandale - Tue Dec 07 11:53:10 EST 2010

Since callback functions routinely deal with DBEs, you should routinely declare your DB and DBE variables globally, unless your box is very simple and doesn't require anything tricky. Also, forgive ignorance but I suspect there may be an issue with declaring your xValChoice[] vector inside the function, as its de-allocated when the function ends. I'd declare that globally as well.

The following works in an included file.

DB  xVALDB       = null
DBE xVALOpt      = null
DBE xVALSel      = null
DBE applyBtn     = null
string xVALChoice[]  = {"A", "B", "C", "D"}
 
void doSelect(DBE xVALOpt)
{
    string xVAL = get xVALOpt
        set (xVALSel, xVAL)
}
 
string guessAndAsk ()
{
        xVALDB = create("", styleCentered|styleFloating)
        xVALOpt  = list (xVALDB, "", 100, 5, xVALChoice)
        xVALSel = field (xVALDB, "Specific Value: ", "" ,4)
        realize xVALDB
        set (xVALOpt, doSelect)
        block xVALDB
        string RetValue = get xVALSel
        hide(xVALDB)
        destroy(xVALDB)
        xVALDB = null
        return RetValue
}


Now lets not get whipetty-snippetty. This will not work if your included file is inside some function in the main code, since the 'global' variables in the include will no longer be 'global' and your functions, strangely, can access only local or global variables, they cannot access variables declared in an intermediate context. That's a strange 'feature' of DXL. Anyway, you won't need to: $include this in the main and call guessAndAsk from some function will work.

Now of course this #include file will need to insure its global variables don't accidentally conflict with the main calling variables. You may want to adopt a naming convention, perhaps all your library functions start with "f" and all your library variables start with "cl_" or "gl_".

 

 

  • Louie

 

 

Louie,

You are correct and I would normally always declare my DB and DBE variables globally however the code is #included from a function which is called by a trigger.

Maybe I can declare the variables as global by inserting them in the trigger code before the #include command?

Martin
 

bool doCloseTrig (Trigger t)
 {
    DB  xVALDB       = null
    DBE xVALOpt      = null
    DBE xVALSel      = null
    DBE applyBtn     = null
    string xVALChoice[]  = {"A", "B", "C", "D"}
    #include <FILE 1>
}

Re: Forward Declare #include
llandale - Tue Dec 07 15:21:48 EST 2010

Martin_Hunter - Tue Dec 07 12:33:34 EST 2010

Louie,

You are correct and I would normally always declare my DB and DBE variables globally however the code is #included from a function which is called by a trigger.

Maybe I can declare the variables as global by inserting them in the trigger code before the #include command?

Martin
 

bool doCloseTrig (Trigger t)
 {
    DB  xVALDB       = null
    DBE xVALOpt      = null
    DBE xVALSel      = null
    DBE applyBtn     = null
    string xVALChoice[]  = {"A", "B", "C", "D"}
    #include <FILE 1>
}

What you wrote here won't work since the code included in 'File 1' won't be able to access your variables e.g. xVALDB since they are not defined globally, they are in the function.

If your trigger is a dynamic one then you can still #include the file I wrote earlier, and inside this function then just issue the call to function guessAndAsk(), at the spot you have the #include statement.

If your trigger is a stored one, then likewise the trigger code would #include at the top context and again your close function would call guessAndAsk().

However, using an #include in a trigger code causes trouble unless EVERYONE who uses your database can access the server file location specified in the #include. That works for small databases but not for large distributed ones. (Likewise for Layout and AttrDXL code using #include).

#include <FILE 1>
bool doCloseTrig (Trigger t)
{  guessAndAsk()
}


Again, here's the contents of File1

 

DB  xVALDB       = null
DBE xVALOpt      = null
DBE xVALSel      = null
DBE applyBtn     = null
string xVALChoice[]  = {"A", "B", "C", "D"}
 
void doSelect(DBE xVALOpt)
{
    string xVAL = get xVALOpt
        set (xVALSel, xVAL)
}
 
string guessAndAsk ()
{
        xVALDB = create("", styleCentered|styleFloating)
        xVALOpt  = list (xVALDB, "", 100, 5, xVALChoice)
        xVALSel = field (xVALDB, "Specific Value: ", "" ,4)
        realize xVALDB
        set (xVALOpt, doSelect)
        block xVALDB
        return get xVALSel
}

 

 

  • Louie